//
//  UIView+ZDCommon.m
//  ZDEngineering
//
//  Created by kk on 2021/7/5.
//

#import "UIView+FMCommon.h"

@implementation UIView (FMCommon)

#pragma mark -
#pragma mark - 关于位置

/**
 * 获取视图 x 坐标
 *
 *  @return x 坐标
 */
- (CGFloat)getX {
    return self.frame.origin.x;
}
/**
 * 获取视图 y 坐标
 *
 *  @return y 坐标
 */
- (CGFloat)getY {
    return self.frame.origin.y;
}
/**
 * 获取视图 宽度
 *
 *  @return 宽度
 */
- (CGFloat)getWidth {
    return self.frame.size.width;
}
/**
 * 获取视图 高度
 *
 *  @return 高度
 */
- (CGFloat)getHeight {
    return self.frame.size.height;
}
/**
 * 获取视图 最右边距屏幕左边的距离
 *
 *  @return 距离
 */
- (CGFloat)getMaxX {
    return CGRectGetMaxX(self.frame);
}
/**
 * 获取视图 最下边距屏幕左边的距离
 *
 *  @return 距离
 */
- (CGFloat)getMaxY {
    return CGRectGetMaxY(self.frame);
}
/**
 *  获取视图 中心点 X
 *
 *  @return center.x
 */
- (CGFloat)getMidX {
    return self.center.x;
}
/**
 *  获取视图 中心点 Y
 *
 *  @return center.y
 */
- (CGFloat)getMidY {
    return self.center.y;
}

- (void)setCornerRadius:(CGFloat)cornerRadius rectCorner:(UIRectCorner)rectCorner
{
    [self layoutIfNeeded];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = cornerRadius;
}

- (void)setCornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)width borderColor:(UIColor*)color
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
}



- (UIScrollView *)findSuperViewWithView:(UIView *)view tag:(NSInteger)tag
{
    UIView * scrollView = view.superview;
    if (![scrollView isKindOfClass:[UIScrollView class]]) {
        scrollView = [self findSuperViewWithView:scrollView tag:tag];
    }else {
        if (scrollView.tag != tag) {
            scrollView = [self findSuperViewWithView:scrollView tag:tag];
        }
    }
    return (UIScrollView *)scrollView;
}


- (void)gradientLayer:(NSArray *)colors
{
    CAGradientLayer* gradinentlayer = [CAGradientLayer layer];
    gradinentlayer.colors = colors;
    gradinentlayer.locations= @[@0.0,@1.0];
    gradinentlayer.startPoint = CGPointMake(0, 0.52);
    gradinentlayer.endPoint = CGPointMake(1, 0.52);
    gradinentlayer.frame = self.bounds;
    [self.layer insertSublayer:gradinentlayer atIndex:0];
}

-(void)removeGradientLayer{
    
    NSArray *subLayers = self.layer.sublayers;

            if(subLayers.count == 0) {

                return;

            }
    
    NSArray * removedLayers = [subLayers filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        
        return [evaluatedObject isKindOfClass:[CAGradientLayer class]];
    }]];
    [removedLayers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
}
@end

